package cn.gson.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.ConstactOrderDAO;
import cn.gson.crm.model.dao.CustomerinfoIDAO;
import cn.gson.crm.model.dao.OrderDetailsDAO;
import cn.gson.crm.model.dao.ProductDao;
import cn.gson.crm.model.mapper.IConstactOrderDAO;
import cn.gson.crm.model.mapper.zjOrderDetailsDAO;
import cn.gson.crm.model.mapper.zjProductDAO;
import cn.gson.crm.model.pojo.ConstactOrder;
import cn.gson.crm.model.pojo.Customerinfo;
import cn.gson.crm.model.pojo.Orderdetails1;
import cn.gson.crm.model.pojo.Product;
import cn.gson.crm.model.vo.ConstactOrderVO;
import cn.gson.crm.model.vo.CustomerVO;
import cn.gson.crm.model.vo.OrderDetailsVO;
import cn.gson.crm.vo.ProductVo;

@Service
@Transactional
public class OrderDetailsService {

	@Autowired
	private OrderDetailsDAO oDao;
	@Autowired
	private CustomerinfoIDAO cIdao;
	@Autowired
	private ConstactOrderDAO cOrderDAO;
	@Autowired
	private ProductDao pDao;
	@Autowired
	private IConstactOrderDAO iDao;
	@Autowired
	private zjProductDAO zDao;
	@Autowired
	private zjOrderDetailsDAO zDao2;

	/**
	 * 获取所有订单明细
	 * 
	 * @return
	 */
	public List<OrderDetailsVO> getAllOrderDetails(int page, int rows) {
		PageRequest pRequest = new PageRequest(page - 1, rows);
		List<OrderDetailsVO> listvo = new ArrayList<>();
		List<Orderdetails1> listpo = oDao.getAllOrderDetails(pRequest);
		listpo.forEach(r -> {
			System.out.println(r);
		});
		if (listpo != null) {
			for (Orderdetails1 or : listpo) {
				listvo.add(returnOrderDetailsVO(or));
			}
		}
		return listvo;
	}
	
	public OrderDetailsVO getodById(Long id) {
		return returnOrderDetailsVO(oDao.findOne(id));
	}

	public OrderDetailsVO returnOrderDetailsVO(Orderdetails1 or) {
		OrderDetailsVO vo = new OrderDetailsVO(or.getOdId(), or.getOdCusName(), or.getOdProName(), or.getOdPrice(),
				or.getOdPlanTime(), or.getOdNumber(), or.getOdOwner(), or.getOdIfSay(), or.getOdProType(),
				or.getOdRemarks());
		
		Product p = or.getProduct();
		if(p!=null) {
			ProductVo pVo = new ProductVo(p.getProductId(), p.getProInfoCost(), p.getProPrice(), p.getUnitId(),
					p.getProInfoSta(), p.getProSta(), p.getProductName());
			vo.setProId(pVo.getProductId());
			vo.setProName(pVo.getProductName());
			vo.setProductVo(pVo);
		}
		
		ConstactOrder cOrder = or.getConstactOrder();
		if(cOrder!=null) {
			ConstactOrderVO cVo = new ConstactOrderVO(cOrder.getCoId(), cOrder.getCoTheme());
			vo.setCoIdvo(cVo.getCoId());
			vo.setCoThemezj(cVo.getCoTheme());
			vo.setcVo(cVo);
		}

		Customerinfo customerinfo = or.getCustomerinfo();
		if(customerinfo!=null) {
			CustomerVO cVo2 = new CustomerVO(customerinfo.getCustId(), customerinfo.getCustName());
			vo.setCusIdvo(cVo2.getCustId());
			vo.setCusNamevo(cVo2.getCustName());
			vo.setCustomerVO(cVo2);
		}
		return vo;
	}

	/**
	 * 获取订单明细总条数
	 * 
	 * @return
	 */
	public long orderCount() {
		return oDao.count();
	}
	
	/**
	 * 删除订单明细
	 * 
	 * @return
	 */
	public void deleteOd(Long id) {
		Orderdetails1 cOrder = oDao.findOne(id);
		oDao.delete(cOrder);
	}
	
	/**
	 * 批量删除
	 * @param id
	 */
	public void deleteSome(Long row[]) throws Exception{
		try {
			for(Long long1:row) {
				oDao.delete(long1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 添加订单明细
	 * 
	 * @param od
	 * @param custId
	 * @param coId
	 * @param proId
	 */
	public void addOrderDetails(Orderdetails1 od, Long custId, Long coId, Long proId) {
		Customerinfo customerinfo = cIdao.findOne(custId);
		ConstactOrder constactOrder = cOrderDAO.findOne(coId);
		Product product = pDao.findOne(proId);
		if (constactOrder != null) {
			od.setConstactOrder(cOrderDAO.findOne(coId));
			//od.setOdCusName(constactOrder.getCoTheme());
		}
		if (product != null) {
			od.setProduct(pDao.findOne(proId));
		}
		if (customerinfo != null) {
			od.setCustomerinfo(cIdao.findOne(custId));
		}
		od.setOdProName(product.getProductName());
		oDao.save(od);
	}

	public List<ConstactOrder> getByOrderId(Integer id) {
		return iDao.findByOrderId(id);
	}

	public List<Product> findProByOrderId(Long id) {
		return zDao.findProByOrderId(id);
	}
	
	public List<Product> findProByOrder(Long id) {
		return zDao.findProByOrder(id);
	}

	/**
	 * 根据订单id查询订单明细
	 * 
	 * @param id
	 * @return
	 */
	public List<OrderDetailsVO> getOrderDetailsById(Long id) {
		List<Orderdetails1> listpo = zDao2.getOrderDetailsById(id);
		List<OrderDetailsVO> listvo = new ArrayList<>();
		listpo.forEach(r -> {
			System.out.println(r);
		});
		if (listpo != null) {
			for (Orderdetails1 or : listpo) {
				listvo.add(returnOrderDetailsVO(or));
			}
		}
		return listvo;	
	}
	
	public List<Orderdetails1> getDetailsByOid(Long id){
		return zDao2.getOrderDetailsById(id);
	}
}
